package cn.ssm.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.ssm.biz.impl.ClassroomBizImpl;
import cn.ssm.biz.impl.CompInfoBizImpl;
import cn.ssm.biz.impl.ComputerroomBizImpl;
import cn.ssm.biz.impl.FaultBizImpl;
import cn.ssm.biz.impl.RepairListBizImpl;
import cn.ssm.biz.impl.TeacherBizImpl;
import cn.ssm.biz.impl.UserBizImpl;
import cn.ssm.pojo.Class;
import cn.ssm.pojo.Computerinfo;
import cn.ssm.pojo.Computerroom;
import cn.ssm.pojo.Fault;
import cn.ssm.pojo.Repairlist;
import cn.ssm.pojo.Teacher;
import cn.ssm.pojo.User;
import cn.ssm.query.ComputerQuery;
@Controller
@RequestMapping("/computerinfo")
public class ComputerinfoController {
	@Autowired
	@Qualifier("computerroomBizImpl")
	private ComputerroomBizImpl com;
	public ComputerroomBizImpl getCom() {
		return com;
	}
	public void setCom(ComputerroomBizImpl com) {
		this.com = com;
	}

	
	
	@Autowired
	@Qualifier("FaultBizImpl")
	private FaultBizImpl faultBizImpl;
	@Autowired
	@Qualifier("RImpl")
	private RepairListBizImpl repBizimpl;
	
	@Autowired
	@Qualifier("UBizImpl")
	private UserBizImpl UserBizimpl;
	
	@Autowired
	private TeacherBizImpl teacherBizImpl;
	
	@Autowired
	private ClassroomBizImpl classroomBizImpl;
	@Autowired
	@Qualifier("CImpl")
	private CompInfoBizImpl CIBI;
	
	@RequestMapping("/show")
	public String selCRID(Model model,int computerroomId){
		List<Computerinfo> l=CIBI.selectCompInfo(computerroomId); 
		model.addAttribute("list",l);
		System.out.println("===================================>>>>>>>>>>>>"+l.size());
		return "CR_One";
	}
	
	
	@RequestMapping("/show1")
	public String selCRID1(Model model,int computerroomId,HttpSession session){
		List<Computerinfo> l=CIBI.selectCompInfo(computerroomId); 
		model.addAttribute("list",l);
		Computerroom computerroom = com.selComNameById(computerroomId).get(0);
		model.addAttribute("computerroom", computerroom);
		User user = (User) session.getAttribute("user");
		statusConvert(l,computerroomId,user);

		return "CR_One2";
	}
	
	
	
	/**
	 * 转换状态
	 * @param list
	 * @param computerroomId
	 * @param user
	 */
	private void statusConvert(List<Computerinfo> list,Integer computerroomId,User user) {
		
		List<Repairlist> findRepairlistAllByComputerroomId = repBizimpl.findRepairlistAllByComputerroomId(computerroomId);
		
		
		boolean temp =true;
		
		if(user.getRoName().equals("老师") || user.getRoName().equals("管理员")) {
			temp=false;
			
		}
		
		
		for (int i = 0; i < 2; i++) {

			if (temp) {

				for (Computerinfo computerinfo : list) {

					for (Repairlist repairlist : findRepairlistAllByComputerroomId) {

						if (computerinfo.getComputerId() == repairlist.getComputerId()
								&& repairlist.getStatusId() == 2) {

							computerinfo.setDescribe("待审核");
						}

					}

				}
				
				temp=false;

			} else {

				for (Computerinfo computerinfo : list) {

					for (Repairlist repairlist : findRepairlistAllByComputerroomId) {

						if (computerinfo.getComputerId() == repairlist.getComputerId()
								&& repairlist.getStatusId() == 1) {

							computerinfo.setDescribe("损坏");
						}

					}

				}
				
				temp=true;
			}

		}
		
	}
	

	
	
	
	
	@RequestMapping("/findAddList")
	public String findAddList(ModelMap mo,@RequestParam(value = "computerId", required = false)Integer computerId,Integer computerroomId) {
		
		
		System.out.println("=======================computerId====================================="+computerId);
		
		List<Fault> faultList=faultBizImpl.faultInfo1();
		mo.put("faultList",faultList);

		List<Teacher> teacherList = teacherBizImpl.getTeacherList1();
		List<Class> classroomInfo = classroomBizImpl.classroomInfo1();
		List<User> userList=UserBizimpl.selectUsersInfo1();
		List<Computerroom> computerroom=com.getRoomdescribe();
		System.out.println("roomlise"+computerroom.size());
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String gmtString = format.format(new Date());
		String periodName = getPeriodName(new Date());
		
		ComputerQuery selComputer = CIBI.selComputer(periodName, computerroomId, computerId);
		

		
		mo.put("teacherList",teacherList);
		mo.put("computerroom", computerroom);
		mo.put("classroomInfo",classroomInfo);
		mo.put("computerId",computerId);
		mo.put("computerroomId",computerroomId);
		mo.put("faultTime",gmtString);
		mo.put("selComputer",selComputer);

		
		if(computerId==null) {
			
			return "addicheck";
		}
		
		
//		System.out.println("================computerinfo=======findAddList=======   "+computerroomId+"   "+gmtString+"    "+selComputer.toString());
		return "addicheck2";
		
		
	}
	
	
	
	public  String getPeriodName(Date dt) {
		
		String dataStr = "";
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
      
        
        if (w < 0)
            w = 0;
        
       switch (w) {
		case 1:
		case 3:
		case 5:
			dataStr = "一三五";
			break;
		case 2:
		case 4:
		case 6:
			dataStr = "二四六";
			break;
		default:
			dataStr = "星期日";
			break;
		}
        
       SimpleDateFormat sdf=new SimpleDateFormat("HH");
       Integer HH = Integer.parseInt(sdf.format(dt));
       if(HH>=13) {
    	   return dataStr+"下午";
    	   
       }else {
    	   return dataStr+"上午";
    	   
       }
       
	}
	
//@Autowired
//@Qualifier("CImpl")
//	private ComputerinfoBizImpl ctbi;
//	public ComputerinfoBizImpl getCtbi() {
//	return ctbi;
//}
//public void setCtbi(ComputerinfoBizImpl ctbi) {
//	this.ctbi = ctbi;
//}
//	public String selcpt(){
//		Computerinfo comp = new Computerinfo();
//		comp.setComputerId(1);
//		comp.setComputerroomId(5);
//		Computerinfo c=ctbi.addComputerinfo(comp);
//		
//		return null;
//		
//	}
	
	
//	@RequestMapping("/show")
//	public String test (Model model,String userName){
//		List<Computerinfo> l=ctbi.selectCompInfo("张三");
//		model.addAttribute("list",l);
//		return "CR_One";
//	}
//	
//	@RequestMapping("/tc")
//	public String selecttc (Model model,String userName){
//		List<Computerinfo> l=ctbi.selectCompInfo("张老师");
//		model.addAttribute("list",l);
//		return "CR_One"; 
//	}
public static void main(String[] args) {
	
	ComputerinfoController ci=new ComputerinfoController();
	System.out.println("hh");
	Calendar cal = Calendar.getInstance();
	cal.add(Calendar.HOUR,12);
	System.out.println(ci.getPeriodName(cal.getTime()));
	
	
}
	
}
